/* eslint-disable no-nested-ternary */
import moment from 'moment';
import {uuid} from '../utils';
/**
 *  转换ant table的sorter对象转换成分页的查询的排序字段
 * @param {object} sorter
 */
export const conventSorter = sorter => {
    const option = {};
    if(sorter)
        // 点击排序
        if(sorter.order && sorter.field) {
            option.sortField = sorter.field;
            if(sorter.order === 'descend')
                option.isDesc = true;
            else
                option.isDesc = false;
        } else { // 取消排序
            option.sortField = undefined;
            option.isDesc = undefined;
        }
    return option;
};

export const hasPermissions = (data, name) => Array.isArray(data) && data.length > 0 && data.some(item => item === name);
export const addMonths = (n, time) => {
    const v = time ? new Date(time) : new Date();
    if(v) {
        const t = new Date(v.getFullYear(), v.getMonth(), v.getDate(), v.getHours(), v.getMinutes(), v.getSeconds(), v.getMilliseconds());
        return moment(t.setMonth(v.getMonth() - (n + 1))).format('Y-MM');
    }
};
export const polylinelist = (n, data, y) => {
    const box = [];
    if(data.length > 0)
        // eslint-disable-next-line array-callback-return
        data.map(item => {
            const list = n === 1 ? [{
                id: uuid(),
                month: addMonths(10, y),
                value: item.n1,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(9, y),
                value: item.n2,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(8, y),
                value: item.n3,
                year: item.dealerCode.toString()
            }, {
                month: addMonths(7, y),
                value: item.n4,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(6, y),
                value: item.n5,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(5, y),
                value: item.n6,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(4, y),
                value: item.n7,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(3, y),
                value: item.n8,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(2, y),
                value: item.n9,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(1, y),
                value: item.n10,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(0, y),
                value: item.n11,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(-1, y),
                value: item.n12,
                year: item.dealerCode.toString()
            }] : n === 2 ? [{
                id: uuid(),
                month: addMonths(4, y),
                value: item.n7,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(3, y),
                value: item.n8,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(2, y),
                value: item.n9,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(1, y),
                value: item.n10,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(0, y),
                value: item.n11,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(-1, y),
                value: item.n12,
                year: item.dealerCode.toString()
            }] : [{
                id: uuid(),
                month: addMonths(1, y),
                value: item.n10,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(0, y),
                value: item.n11,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(-1, y),
                value: item.n12,
                year: item.dealerCode.toString()
            }];
            box.push(...list);
        });
    return box;
};
export const polylinelistCycle = (n, data, y) => {
    const box = [];
    if(data.length > 0)
        // eslint-disable-next-line array-callback-return
        data.map(item => {
            const list = n === 1 ? [{
                id: uuid(),
                month: addMonths(10, y),
                value: item.n1,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(9, y),
                value: item.n2,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(8, y),
                value: item.n3,
                year: item.dealerCode.toString()
            }, {
                month: addMonths(7, y),
                value: item.n4,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(6, y),
                value: item.n5,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(5, y),
                value: item.n6,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(4, y),
                value: item.n7,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(3, y),
                value: item.n8,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(2, y),
                value: item.n9,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(1, y),
                value: item.n10,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(0, y),
                value: item.n11,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(-1, y),
                value: item.n12,
                year: item.dealerCode.toString()
            }] : n === 2 ? [{
                id: uuid(),
                month: addMonths(4, y),
                value: item.n7,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(3, y),
                value: item.n8,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(2, y),
                value: item.n9,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(1, y),
                value: item.n10,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(0, y),
                value: item.n11,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(-1, y),
                value: item.n12,
                year: item.dealerCode.toString()
            }] : [{
                id: uuid(),
                month: addMonths(1, y),
                value: item.n10,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(0, y),
                value: item.n11,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(-1, y),
                value: item.n12,
                year: item.dealerCode.toString()
            }];
            box.push(...list);
        });
    return box;
};
export const polylinelistLink = (n, data, y) => {
    const box = [];
    if(data.length > 0)
        // eslint-disable-next-line array-callback-return
        data.map(item => {
            const list = n === 1 ? [{
                id: uuid(),
                month: addMonths(10, y),
                value: item.n1,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(9, y),
                value: item.n2,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(8, y),
                value: item.n3,
                year: item.dealerCode.toString()
            }, {
                month: addMonths(7, y),
                value: item.n4,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(6, y),
                value: item.n5,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(5, y),
                value: item.n6,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(4, y),
                value: item.n7,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(3, y),
                value: item.n8,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(2, y),
                value: item.n9,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(1, y),
                value: item.n10,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(0, y),
                value: item.n11,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(-1, y),
                value: item.n12,
                year: item.dealerCode.toString()
            }] : n === 2 ? [{
                id: uuid(),
                month: addMonths(4, y),
                value: item.n7,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(3, y),
                value: item.n8,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(2, y),
                value: item.n9,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(1, y),
                value: item.n10,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(0, y),
                value: item.n11,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(-1, y),
                value: item.n12,
                year: item.dealerCode.toString()
            }] : [{
                id: uuid(),
                month: addMonths(1, y),
                value: item.n10,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(0, y),
                value: item.n11,
                year: item.dealerCode.toString()
            }, {
                id: uuid(),
                month: addMonths(-1, y),
                value: item.n12,
                year: item.dealerCode.toString()
            }];
            box.push(...list);
        });
    return box;
};
